<template>
  <basic-container :btnFlag=true>
    <div class="card-border">
      <card-header :header-data="headerData"></card-header>
      <el-form
        label-width="130px"
        :model="subjectForm"
        style="padding-right: 30px;"
        :rules="subjectInfosrules"
        ref="subjectForm"
      >
        <el-row>
          <el-col :span="22" style="margin-top: 27px">
            <el-form-item class="form-item" label="计划方案名称 :" prop="title">
              <span>{{subjectForm.title}}</span>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="22">
            <el-form-item class="form-item" label="有关说明 :" prop="description">
              <span>{{subjectForm.description}}</span>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div style="margin-left: 14px;margin-bottom: -13px;font-weight: 600">
        <span>课题计划方案</span>
      </div>
      <div class="card-body">
        <avue-crud :data="subjectListData"
                   :option="tableoption"
                   :page="page"
                   :table-loading="tableLoading"
                   @on-load="getResultList"
                   @size-change="sizeChange"
                   @current-change="currentChange"
                   class="avuecrud">
          <template slot-scope="scope" slot="menu">
            <div class="menu-title" @click.stop="planFeedback(scope.row,scope.index)">
              <i class="iconfont iconview icon-primary"></i>
              <p class="menu-title-text">查看反馈</p>
            </div>
            <div class="menu-title" @click.stop="subjectOpinionDelete(scope.row,scope.index)">
              <i class="iconfont iconalter icon-warning"></i>
              <p class="menu-title-text">删除</p>
            </div>
          </template>
        </avue-crud>
      </div>
      <div slot="footer" class="btn-footer">
        <el-button type="success" @click="exportPlanFile()">导出方案</el-button>
        <el-button type="primary" @click="releasePlan()">下达计划</el-button>
        <el-button @click="cancel()">取 消</el-button>
      </div>
    </div>
  </basic-container>
</template>

<script>
  import {getPlanSubjectList,releasePlan,exportSubjectAssignDept} from "@/api/subject";
  import {filedownload} from "@/api/system";

  export default {
    name: "releasePlan",
    created(){
      this.subjectForm.title = this.$route.query.title;
      this.subjectForm.description = this.$route.query.description;
      this.putSubjectListData();
    },
    data(){
      return{
        headerData: {
          title: '下达计划'
        },
        subjectListData:[],
        page: {
          total: 0, // 总页数
          currentPage: 1, // 当前页数
          pageSize: 10 // 每页显示多少条
        },
        tableLoading:false,
        tableoption: {
          page: true,
          menuWidth: 220,
          addBtn: false,
          editBtn: false,
          addRowBtn: false,
          indexLabel: "序号",
          index: true,
          indexWidth:70,
          cellBtn: false,
          delBtn: false,
          columnBtn: false,
          refreshBtn: false,
          filterBtn: false,
          searchBtn: false,
          keyId: "id",
          menu:false,
          column: [
            {
              label: "课题名称",
              width: 250,
              prop: "title",
              cell: true
            },
            {
              label: "课题概述",
              width: 330,
              prop: "description",
              cell: true
            },
            {
              label: "牵头单位",
              prop: "deptName",
              cell: true,
            },
            {
              label: "配合单位",
              prop: "supportDeptName",
              cell: true,
            },
          ]
        },
        subjectOpinionListData:[],
        pageOpinion: {
          total: 0, // 总页数
          currentPage: 1, // 当前页数
          pageSize: 10 // 每页显示多少条
        },
        tableLoadingOpinion:false,
        tableoptionOpinion: {
          page: true,
          menuWidth: 120,
          addBtn: false,
          editBtn: false,
          addRowBtn: false,
          indexLabel: "序号",
          index: true,
          indexWidth:70,
          cellBtn: false,
          delBtn: false,
          columnBtn: false,
          refreshBtn: false,
          filterBtn: false,
          searchBtn: false,
          keyId: "id",
          column: [
            {
              label: "计划名称",
              width: 150,
              prop: "nickname",
              cell: true
            },
            {
              label: "计划日期",
              width: 150,
              prop: "nickname",
              cell: true
            },
            {
              label: "反馈部门",
              width: 150,
              prop: "nickname",
              cell: true
            },
            {
              label: "反馈结果",
              width: 300,
              prop: "title",
              cell: true
            },
            {
              label: "反馈时间",
              prop: "type",
              cell: true,
            },
          ]
        },
        subjectForm:{

        },
      }
    },
    methods: {
      currentChange(currentPage) {
        var page = this.page;
        page.currentPage = currentPage;
        this.getResultListPage(page);
      },
      sizeChange(pageSize) {
        var page = this.page;
        page.pageSize = pageSize;
        this.getResultListPage(page);
      },
      getResultListPage(page) {
        getPlanSubjectList(Object.assign({
          current: page.currentPage,
          size: page.pageSize,
          planId:this.$route.params.id,
        })).then(response=>{
          this.subjectListData = response.data.data.records;
          this.page.total = response.data.data.total;
        })
      },
      putSubjectListData(){
        getPlanSubjectList(Object.assign({
          current: 1,
          size: 10,
          planId:this.$route.params.id,
        })).then(response=>{
          this.subjectListData = response.data.data.records;
          this.page.total = response.data.data.total;
        })
      },
      getResultList() {

      },
      searchChangeOpinion() {

      },
      refreshChangeOpinion() {

      },
      getResultListOpinion(page) {

      },
      exportPlanFile(){
        this.$confirm("确定要导出方案吗?", "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(() => {
          exportSubjectAssignDept(this.$route.params.id).then(response => {
            var exportFileName = this.$route.query.achievementTitle + "课题计划方案.docx";
            this.download(response.data, exportFileName)
          });
        })
      },
      // 附件下载
      downloadFile(fileId, fileName) {
        filedownload(fileId).then(response => {
          if (response.status == 200) {
            this.download(response.data, fileName)
          }
        }).catch((error) => {
          this.$notify({
            title: "失败",
            message: "上传失败[" + error + "]",
            type: "error",
            duration: 2000
          });
        })
      },
      // 下载文件
      download(data, filename) {
        if (data == null) {

        } else {
          let url = window.URL.createObjectURL(new Blob([data]));
          let link = document.createElement('a');
          link.style.display = 'none';
          link.href = url;
          link.setAttribute('download', filename);
          document.body.appendChild(link);
          link.click()
        }
      },
      cancel(){
        this.$router.go(-1)
      },
      releasePlan(){
        this.$confirm("确定要下达计划吗?", "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(() => {
            releasePlan(this.$route.params.id).then(response =>{
              if(response.data.code == 0){
                this.$notify({
                  title: '成功',
                  message: '下达计划成功',
                  type: 'success',
                  duration: 2000
                });
                this.$router.go(-1)
              }
            })
        })
      },
    }

  }
</script>

<style lang="scss" scoped>
  .btn-footer{
    text-align: center;
    margin-bottom: 20px;
  }
  .form-item{
    font-weight: 600;
  }
</style>

